<?php
/**
 * Created by PhpStorm.
 * User:lyq
 * Date: 2018/8/22
 * Time: 13:49
 */

namespace app\bussiness\controller;

use think\Exception;
use think\Db;
class Notice extends Common
{
    public function index(){
        //标题
        $this->templateTitle("公告列表");
        $where['bussiness_id'] = $this->currentBussinessId();
        $where['id'] = ['gt',0];

        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["name"] = ["LIKE", "%$condition%"];
        }

        //获取所有用户
        $list = Db::name("notice")
            ->where($where)
            ->order("display_order DESC,update_time DESC")
            ->paginate(10);

        // echo Db::name("admin_role")->getLastSql();
        $this->assign("list", $list);

        return $this->fetch();
    }
    //添加
    public function add(){
        if (request()->isGet())
        {
            $this->layoutModal();
            $this->templateTitle('添加公告');

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "bussiness_id"=>$this->currentBussinessId(),
                "create_time" => now_datetime(),
                "update_time" => now_datetime()
            );
            //排序
            $data["display_order"] = request()->param("ord");
            $data["name"] = request()->param("name");
            //是否显示
            $data["is_visible"] = request()->param("visible");
            $detail = request()->param("article_detail");
            $data["detail"] =  $detail;


            $result = Db::name('notice')
                ->insert($data);
            if ($result)
            {
                return $this->ajaxSuccess("添加成功！");
            }
            else
            {
                return $this->ajaxError("添加失败！");
            }
        }
    }
    //编辑
    public function edit(){
        //ID
        $Id = request()->param("aid");
        $bussiness_id = $this->currentBussinessId();
        if (request()->isGet())
        {
            $this->layoutModal();

            //获取信息
            $type= Db::name("notice")
                ->where("id", $Id)
                ->where('bussiness_id',$bussiness_id)
                ->find();
            $this->assign("type", $type);
            ///$this->assign("single_image", array("thumb"=>$type["thumb"]));
            //处理详细内容
            $detail =$type["detail"];
            $this->assign("ueditor_content", array("article_detail"=>$detail));
            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //paixu
            $data["display_order"] = request()->param("ord");
            $data["name"] = request()->param("name");
            //图标
           // $data["thumb"] = request()->param("thumb");
            //是否显示
            $data["is_visible"] = request()->param("visible");
            //文章内容
            $detail = request()->param("article_detail");
            $data["detail"] =$detail;
            try
            {


                $save = Db::name("notice")
                    ->where("id", $Id)
                    ->where('bussiness_id',$bussiness_id)
                    ->update($data);
                if($save){
                    return $this->ajaxSuccess("更新成功！");
                }else{
                    return $this->ajaxError("更新失败！");
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }
    //删除
    public function removeNotice(){
        if (request()->isAjax())
        {
            //获取ID
            $Id = request()->param("rid");
            $bussiness_id = $this->currentBussinessId();
            // 删除
            $result = Db::name('notice')
                ->where('id', $Id)
                ->where('bussiness_id',$bussiness_id)
                ->delete();
            if($result){
                return $this->ajaxSuccess("删除成功！");
            }else{
                return $this->ajaxError("删除失败！");
            }

        }
    }
}